// views/index/shop/mailservice/second/index.js
var BASE_URL = getApp().globalData.BASE_URL
var area = require('../../../../../utils/area');
var util = require('../../../../../utils/util');

import Notify from '../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList: area.default,
    showarea: false, //地区遮罩
    area: '',
    name: '',
    name_pinyin: '',
    phone: '',
    wechat: '',
    address: '',
    dizhilist: '',
    userid: '',
    useremail: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        userid: userinfo.id
      })
      _this.getdizhilist()
    }
  },
  //获得地址
  getdizhilist: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/dizhi`,
      method: "GET",
      data: {
        id: _this.data.userid
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          dizhilist: res.data
        })
      }
    })
  },

  //使用地址
  userdizhi: function (e) {
    console.log(e)
    var dizhi = this.data.dizhilist[e.currentTarget.dataset.index]
    this.setData({
      name: dizhi.sname,
      name_pinyin: dizhi.pingyin,
      phone: dizhi.tel,
      wechat: dizhi.wechat,
      area: dizhi.qvyu,
      address: dizhi.dizhi,
      useremail: dizhi.email
    })
  },
  //删除地址
  deldizhi: function (e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定删除此地址吗?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/gongzheng/deletedizhi`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              _this.getdizhilist()
            }
          })
        } else if (res.cancel) {}
      }
    })
  },

  //填写信息的下一步按钮
  gothird: function () {
    var _this = this
    if (!_this.ifhanzi(_this.data.name)) {
      Notify('姓名输入错误');
    }

    if (!_this.ifwewhat(_this.data.wechat)) {
      Notify('微信号输入错误');
    }
    if (!_this.codeVerification(_this.data.phone)) {
      Notify('请输入正确的手机号');
    }

    if (!_this.ifenglish(_this.data.name_pinyin)) {
      Notify('拼音输入错误');
    }
    if (_this.data.address == "") {
      Notify('详细地址不能为空');
    }

    if (_this.ifhanzi(_this.data.name) &&
      _this.ifwewhat(_this.data.wechat) && _this.codeVerification(_this.data.phone) && _this.ifenglish(_this.data.name_pinyin) && _this.data.address !== ""
    ) {
      wx.request({
        url: `${BASE_URL}/dhl/mobileinfo`,
        method: "POST",
        data: {
          username: _this.data.name,
          sname: _this.data.name_pinyin,
          tel: _this.data.phone,
          wechat: _this.data.wechat,
          address: _this.data.area,
          address_detail: _this.data.address,
          email: _this.data.useremail,
          uid: _this.data.userid
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.navigateTo({
            url: `../third/index?id=${res.data}`
          })
        }
      })

    }
  },














  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },



  //打开地区选择遮罩层
  open_area: function (e) {
    this.setData({
      showarea: true
    })
  },
  //选择好地区并关闭地区选择器遮罩呈
  changeuserarea: function (e) {
    console.log(e)
    var str = []
    e.detail.values.forEach((item, index) => {
      if (item) {
        str.push(item.name)
      }
    })
    str = str.join('--')
    this.setData({
      showarea: false,
      area: str
    })
  },
  //关闭遮罩
  closeuserarea: function () {
    this.setData({
      showarea: false
    })
  },



})